package com.hph.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hph.health.common.BaseResponse;
import com.hph.health.common.ErrorCode;
import com.hph.health.common.ResultUtils;
import com.hph.health.exception.BusinessException;
import com.hph.health.exception.ThrowUtils;
import com.hph.health.model.dto.address.AddUserAddressRequest;
import com.hph.health.model.dto.address.AddressRequest;
import com.hph.health.model.dto.product.ProductUpdateRequest;
import com.hph.health.model.dto.user.DeleteRequest;
import com.hph.health.model.entity.Product;
import com.hph.health.model.entity.UserAddress;
import com.hph.health.service.UserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 地址接口
 *
 * @author hanph
 * @from haut
 */
@RestController
@RequestMapping("/adress")
@Slf4j
public class UserAddressController {

    @Resource
    private UserAddressService userAddressService;
    /**
     * 分页获取地址列表
     *
     * @param adressRequest
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<UserAddress>> listUserByPage(@RequestBody AddressRequest adressRequest) {
        long current = adressRequest.getCurrent();
        long size = adressRequest.getPageSize();
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(adressRequest.getId() != null, "user_id", adressRequest.getId());
        Page<UserAddress> userAdressPage = userAddressService.page(new Page<>(current, size),
                queryWrapper);
        return ResultUtils.success(userAdressPage);
    }

    /**
     *  创建地址
     *
     * @param addUserAddressRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addProduct(@RequestBody AddUserAddressRequest addUserAddressRequest) {
        if (addUserAddressRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserAddress address = new UserAddress();
        BeanUtils.copyProperties(addUserAddressRequest, address);
        boolean result = userAddressService.save(address);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(address.getId());
    }

    /**
     * 删除地址
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteProduct(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(userAddressService.removeById(deleteRequest.getId()));
    }

    /**
     * 更新地址
     *
     * @param addUserAddressRequest
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateProduct(@RequestBody AddUserAddressRequest addUserAddressRequest) {
        if (addUserAddressRequest == null || addUserAddressRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserAddress address = new UserAddress();
        BeanUtils.copyProperties(addUserAddressRequest, address);
        boolean result = userAddressService.updateById(address);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

}
